﻿using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace FitNesseRunner
{
	internal class VariablesResolver
	{
		private readonly IDictionary<string, string> _variables;

		public VariablesResolver(IDictionary<string, string> variables)
		{
			_variables = variables;
		}

		public string Resolve(string stringWithVariables)
		{
			var regex = new Regex(@"\$[A-Za-z_]+");
			return regex.Replace(stringWithVariables, ResolveVariable);
		}

		private string ResolveVariable(Match match)
		{
			var variableName = match.Value.Substring(1);
			string value;
			return _variables.TryGetValue(variableName, out value)
			       	? value
			       	: null;
		}
	}
}